package Formularios;

import BD.Validacao;
import BD.produtos;
import BD.Compras;
import BD.Fornecedor;

import BD.utilsPim;
import java.sql.Date;

import java.util.ArrayList;
import javax.swing.JOptionPane;

public class FormCompras extends javax.swing.JFrame {

    Compras compras = new Compras();
    produtos produto = new produtos();
    Fornecedor fornecedor = new Fornecedor();
    ArrayList<produtos> listProd = new ArrayList<produtos>();
    ArrayList<Fornecedor> listForn = new ArrayList<Fornecedor>();
    ArrayList<Compras> listComp = new ArrayList<Compras>();
    utilsPim utlp = new utilsPim();    

    int ponteiro       = 0;
    boolean Incluindo  = false;
    Validacao vldc = new Validacao();
    /**
     * Creates new form FormCompra
     */
    public FormCompras() {
        
        initComponents();
        setLocationRelativeTo(null);
        jcbxproduto.removeAllItems();        
        CarregaCombobox();    
        
        /*int Heigth = jcbxfornecedor.getSize().height,Width = jcbxfornecedor.getSize().width;
        jtEspelhoForn.setAlignmentX(jcbxfornecedor.getAlignmentX());
        jtEspelhoForn.setAlignmentY(jcbxfornecedor.getAlignmentY());                      
*/

        
        /*
        jtEspelhoprod.setSize(jcbxproduto.getWidth(),jcbxproduto.getHeight());
        jtEspelhoprod.setAlignmentX(jcbxproduto.getX());
        jtEspelhoprod.setAlignmentY(jcbxproduto.getY());       
        jtEspelhoForn.setAlignmentX(jcbxfornecedor.getX());
        jtEspelhoForn.setAlignmentY(jcbxfornecedor.getY());              
        
        jcbxfornecedor.setVisible(Incluindo);
        jcbxproduto.setVisible(Incluindo);
        jtEspelhoForn.setVisible(! Incluindo);
        jtEspelhoprod.setVisible(!  Incluindo);
    
    */
    
    
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpnlPesq = new javax.swing.JPanel();
        jbtnPesquisa = new javax.swing.JButton();
        jpNavegacao = new javax.swing.JPanel();
        jbtnFirst = new javax.swing.JButton();
        jbtnPrior = new javax.swing.JButton();
        jbtnNext = new javax.swing.JButton();
        jbtnLast = new javax.swing.JButton();
        jcbxprodutoPesq = new javax.swing.JComboBox();
        labelProdutos1 = new javax.swing.JLabel();
        Separador1 = new javax.swing.JSeparator();
        labelCodigo = new javax.swing.JLabel();
        jtcodigo = new javax.swing.JTextField();
        labelProdutos = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jtQuantidade = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jtValorUN = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jtValorTotal = new javax.swing.JTextField();
        jbtncancelar = new javax.swing.JButton();
        jbtnexcluir = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        jbtnNovo = new javax.swing.JButton();
        jbtnGravar = new javax.swing.JButton();
        jbtnAlterar = new javax.swing.JButton();
        jcbxfornecedor = new javax.swing.JComboBox();
        jcbxproduto = new javax.swing.JComboBox();
        jtData = new javax.swing.JFormattedTextField();
        labelCodigo1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Compras");

        jbtnPesquisa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Pesquisa.png"))); // NOI18N
        jbtnPesquisa.setText("Pesquisar");
        jbtnPesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPesquisaActionPerformed(evt);
            }
        });

        jbtnFirst.setText("<<");
        jbtnFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnFirstActionPerformed(evt);
            }
        });

        jbtnPrior.setText("<");
        jbtnPrior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnPriorActionPerformed(evt);
            }
        });

        jbtnNext.setText(">");
        jbtnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNextActionPerformed(evt);
            }
        });

        jbtnLast.setText(">>");
        jbtnLast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnLastActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpNavegacaoLayout = new javax.swing.GroupLayout(jpNavegacao);
        jpNavegacao.setLayout(jpNavegacaoLayout);
        jpNavegacaoLayout.setHorizontalGroup(
            jpNavegacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpNavegacaoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbtnFirst)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnPrior)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnNext)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtnLast)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpNavegacaoLayout.setVerticalGroup(
            jpNavegacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpNavegacaoLayout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jpNavegacaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnFirst)
                    .addComponent(jbtnPrior)
                    .addComponent(jbtnNext)
                    .addComponent(jbtnLast)))
        );

        jcbxprodutoPesq.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        labelProdutos1.setText("Produtos");

        javax.swing.GroupLayout jpnlPesqLayout = new javax.swing.GroupLayout(jpnlPesq);
        jpnlPesq.setLayout(jpnlPesqLayout);
        jpnlPesqLayout.setHorizontalGroup(
            jpnlPesqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnlPesqLayout.createSequentialGroup()
                .addGroup(jpnlPesqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnlPesqLayout.createSequentialGroup()
                        .addGap(110, 110, 110)
                        .addComponent(jpNavegacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jpnlPesqLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(labelProdutos1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcbxprodutoPesq, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnPesquisa)))
                .addContainerGap())
        );
        jpnlPesqLayout.setVerticalGroup(
            jpnlPesqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnlPesqLayout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jpnlPesqLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnPesquisa)
                    .addComponent(jcbxprodutoPesq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelProdutos1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpNavegacao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        labelCodigo.setText("Código");

        labelProdutos.setText("Produtos");

        jLabel2.setText("Fornecedor");

        jLabel3.setText("Quantidade Unitária");

        jtQuantidade.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtQuantidadeKeyTyped(evt);
            }
        });

        jLabel4.setText("Valor Unitário");

        jtValorUN.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtValorUNKeyTyped(evt);
            }
        });

        jLabel6.setText("Valor Total");

        jtValorTotal.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtValorTotalKeyTyped(evt);
            }
        });

        jbtncancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Cancelar.png"))); // NOI18N
        jbtncancelar.setText("Cancelar");
        jbtncancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtncancelarActionPerformed(evt);
            }
        });

        jbtnexcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Excluir.png"))); // NOI18N
        jbtnexcluir.setText("Excluir");
        jbtnexcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnexcluirActionPerformed(evt);
            }
        });

        jbtnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Incluir.png"))); // NOI18N
        jbtnNovo.setText("Novo");
        jbtnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNovoActionPerformed(evt);
            }
        });

        jbtnGravar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Gravar.png"))); // NOI18N
        jbtnGravar.setText("Gravar");
        jbtnGravar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnGravarActionPerformed(evt);
            }
        });

        jbtnAlterar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Alterar.png"))); // NOI18N
        jbtnAlterar.setText("Alterar");
        jbtnAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAlterarActionPerformed(evt);
            }
        });

        jcbxfornecedor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcbxfornecedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbxfornecedorActionPerformed(evt);
            }
        });

        jcbxproduto.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jtData.setToolTipText("");

        labelCodigo1.setText("Data");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(Separador1)
            .addComponent(jSeparator2)
            .addComponent(jpnlPesq, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jcbxproduto, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jbtnNovo)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbtnAlterar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbtnGravar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbtnexcluir)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jbtncancelar))
                            .addComponent(labelProdutos)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelCodigo)
                                    .addComponent(jtcodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelCodigo1)
                                    .addComponent(jtData, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jcbxfornecedor, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jtQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(59, 59, 59)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtValorUN, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jtValorTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel6))))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jpnlPesq, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(17, 17, 17)
                .addComponent(Separador1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCodigo)
                    .addComponent(jLabel2)
                    .addComponent(labelCodigo1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtcodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbxfornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(labelProdutos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcbxproduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtValorUN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtValorTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtncancelar)
                    .addComponent(jbtnexcluir)
                    .addComponent(jbtnGravar)
                    .addComponent(jbtnAlterar)
                    .addComponent(jbtnNovo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void CarregaCombobox() {
//        produtos        
        jcbxproduto.removeAllItems();
        jcbxprodutoPesq.removeAllItems();
        jcbxfornecedor.removeAllItems();
        
        listProd = produto.getItens();        
        for (int i = 0; i < listProd.size(); i++) {
            jcbxproduto.addItem(listProd.get(i).getDescricao());
            jcbxprodutoPesq.addItem(listProd.get(i).getDescricao());      
        }

        Fornecedor f = new Fornecedor();
        listForn = f.getItens();
        for (int j = 0; j < listForn.size(); j++) {
            jcbxfornecedor.addItem(listForn.get(j).getNome());
        }
        
        jcbxproduto.setSelectedIndex(-1);
        jcbxfornecedor.setSelectedIndex(-1);
        jcbxprodutoPesq.setSelectedIndex(-1);        

    }
    private void limpaCampo() {
        this.jtcodigo.setText("");
        this.jtData.setText("");
        this.jtQuantidade.setText("");
        this.jtValorTotal.setText("");
        this.jtValorUN.setText("");
        this.jcbxfornecedor.setSelectedIndex(-1);
        this.jcbxproduto.setSelectedIndex(-1);
//        this.jtcbPesq.setText("");
/*        
        this.jcbxfornecedor.removeAllItems();
        this.jcbxproduto.removeAllItems();
        this.jcbxprodutoPesq.removeAllItems();
*/        
    }
    private boolean Validacao() {       
//        return (vldc.Valida(String.valueOf(jcbxproduto.getSelectedItem()), "Produto"));
//        return (vldc.Valida(jcbxproduto.getSelectedItem(), "Produto"));
        String   s = "";
        if( jcbxproduto.getSelectedIndex()< 0)        
         s = "";
        else
         s =(String) jcbxproduto.getSelectedItem();
        return (vldc.Valida(s, "Produto") && 
                vldc.Valida(jtValorUN.getText(), "Valor unitario") &&
                vldc.Valida(jtQuantidade.getText(), "Quantidade") &&
                vldc.Valida(jcbxfornecedor.getSelectedIndex()+1, "Fornecedor")
                );
    }
    

    private void TravaBtns(boolean incluindo, boolean navegando, boolean pesquisando) {
        jtcodigo.setEditable(false);
        jtData.setEditable(false);
        jtValorTotal.setEditable(false);        
        
        jtQuantidade.setEditable(incluindo);
        jtValorUN.setEditable(incluindo);
        //jtqtd.setEditable(incluindo);
        //jtunidade.setEditable(incluindo);
        //jtvolume.setEditable(incluindo);
        jpNavegacao.setVisible(!incluindo);
        

        if (incluindo) {
            jbtncancelar.setVisible(true);
            jbtnGravar.setVisible(true);
            jpnlPesq.setVisible(false);
            jbtnexcluir.setVisible(false);
            jbtnNovo.setVisible(false);
            jbtnAlterar.setVisible(false);
        } else if (navegando) {
            jbtnAlterar.setVisible(true);
            jbtncancelar.setVisible(false);
            jbtnGravar.setVisible(false);
            jpnlPesq.setVisible(true);
            jbtnexcluir.setVisible(true);
            jbtnNovo.setVisible(true);
        }

        //jbtnPesquiza.enable(pTipo);
    }
    
    private void movPonteiro(boolean first, boolean prior, boolean next, boolean last) {
        if (listComp.size() > 0) {
            if (first) {
                this.ponteiro = 0;
            } else if (prior) {
                this.ponteiro = this.ponteiro - 1;
                if (this.ponteiro < 0) {
                    this.ponteiro = 0;
                }
            } else if (next) {
                this.ponteiro = this.ponteiro + 1;
                if (this.ponteiro > (listComp.size() - 1)) {
                    this.ponteiro = (listComp.size() - 1);
                }
            } else if (last) {
                this.ponteiro = (listComp.size() - 1);
            }
            
            this.jtcodigo.setText(Integer.toString(listComp.get(this.ponteiro).getId_compras()));
            this.jtQuantidade.setText(Integer.toString(listComp.get(this.ponteiro).getQtd()));            
            this.jtValorUN.setText(Float.toString(listComp.get(this.ponteiro).getValor()));
            this.jtData.setText( utlp.getDataStr(listComp.get(this.ponteiro).getData()));
            
            this.jcbxfornecedor.removeAllItems();
            this.jcbxproduto.removeAllItems();
            
            this.jcbxproduto.addItem(listComp.get(this.ponteiro).getProduto().getDescricao());
            this.jcbxfornecedor.addItem(listComp.get(this.ponteiro).getFornecedor().getNome());
            /*this.jcbxfornecedor.setSelectedIndex(0);
            this.jcbxproduto.setSelectedIndex(0);          
            */
            // calcula o valor total
            this.jtValorTotal.setText(utlp.formatavalor((listComp.get(this.ponteiro).getQtd()*listComp.get(this.ponteiro).getValor() )));         
        } else {
            limpaCampo();
        }
    }

    private void jbtnPesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPesquisaActionPerformed
        if(jcbxprodutoPesq.getSelectedIndex()>=0)
            compras.setDescricao(jcbxprodutoPesq.getSelectedItem().toString());
        listComp = compras.getItens();         // TODO add your handling code here:
        TravaBtns(false, true, true);
        jbtnFirstActionPerformed(evt);
    }//GEN-LAST:event_jbtnPesquisaActionPerformed

    private void jbtnFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnFirstActionPerformed
        movPonteiro(true, false, false, false);
    }//GEN-LAST:event_jbtnFirstActionPerformed

    private void jbtnPriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnPriorActionPerformed
        movPonteiro(false, true, false, false);
    }//GEN-LAST:event_jbtnPriorActionPerformed

    private void jbtnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNextActionPerformed
        movPonteiro(false, false, true, false);
    }//GEN-LAST:event_jbtnNextActionPerformed

    private void jbtnLastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnLastActionPerformed
        movPonteiro(false, false, false, true);
    }//GEN-LAST:event_jbtnLastActionPerformed

    private void jbtncancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtncancelarActionPerformed
        // TODO add your handling code here:
        Incluindo = false;
        TravaBtns(false, true, false);
        limpaCampo();
    }//GEN-LAST:event_jbtncancelarActionPerformed

    private void jbtnexcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnexcluirActionPerformed

        if (listComp.size() <= 0) {
            JOptionPane.showMessageDialog(null, "Nenhum registro Encontrado");
        } else {
            if (JOptionPane.showConfirmDialog(null, "Deseja exluir o compra do produto:" +                
                                                     listComp.get(ponteiro).getProduto().getDescricao()+
                                                     " Na data: "+listComp.get(ponteiro).getData()
                                             ) == 0) 
            {
                if (listComp.get(ponteiro).excluir()) {
                    JOptionPane.showMessageDialog(null, "Operação bem sucedida");
                    limpaCampo();
                    jbtnPesquisaActionPerformed(evt);
                } else {
                    JOptionPane.showMessageDialog(null, "Erro ao excluir o registro");
                }
            }
        }
    }//GEN-LAST:event_jbtnexcluirActionPerformed

    private void jbtnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNovoActionPerformed

        Incluindo = true;
        TravaBtns(true, false, false);        
        limpaCampo();
        jtData.setText(utlp.getData("dd/MM/yyyy"));
        CarregaCombobox();
        jtQuantidade.setText("1");
    }//GEN-LAST:event_jbtnNovoActionPerformed

    private void jbtnGravarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnGravarActionPerformed
        if (Validacao()) {
            Compras compra = new Compras();
            produtos produto = new produtos();
            Fornecedor Fornecedor = new Fornecedor();
            ArrayList<produtos> lstpr = new ArrayList<produtos>();// vai dar a baixa no estoque           
            ArrayList<Fornecedor> lstfr = new ArrayList<Fornecedor>();// vai dar a baixa no estoque           
            
            produto.setDescricao((String) this.jcbxproduto.getSelectedItem());                 
            lstpr = produto.getItens();
            
            fornecedor.setNome((String) this.jcbxfornecedor.getSelectedItem());
            lstfr = fornecedor.getItens();
                    
            /*
            jcbxCodForne.setSelectedIndex(jcbxfornecedor.getSelectedIndex());
            jcbxCodProd.setSelectedIndex(jcbxproduto.getSelectedIndex());           
            */
            compra.setQtd(Integer.parseInt(this.jtQuantidade.getText()));
            compra.setValor(utlp.formatavalorsemmaskS(Float.parseFloat(this.jtValorUN.getText())));           
            compra.setId_fornecedor(lstfr.get(0).getId_fornecedor());
            compra.setId_produto(lstpr.get(0).getId_produtos());
//            compra.setData(Date.valueOf(jtData.getText()));
            
            //pósiciona o cbx codigo para pegar os id's                      

            if (Incluindo) {
                if (compra.incluir()) {
                    TravaBtns(false, true, false);                                        
                    jcbxprodutoPesq.removeAll();
                    jcbxprodutoPesq.addItem(listProd.get(0).getDescricao());
                    jbtnPesquisaActionPerformed(evt);
                    //CarregaCombobox();
                    
                    lstpr.get(0).alterarQuandidade(Integer.parseInt(jtQuantidade.getText()));
                    JOptionPane.showMessageDialog(null, "Operação bem sucedida");
                } else {
                    JOptionPane.showMessageDialog(null, "Erro ao incluir Compras");
                }
            } else {
                compra.setId_compras(Integer.parseInt(this.jtcodigo.getText()));
                if (compra.alterar()) {

                    jbtnPesquisaActionPerformed(evt);

                    JOptionPane.showMessageDialog(null, "Operação bem sucedida");
                } else {
                    JOptionPane.showMessageDialog(null, "Erro ao alterar Compras");
                }
            }

        }
    }//GEN-LAST:event_jbtnGravarActionPerformed

    private void jbtnAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAlterarActionPerformed
        if (listComp.size() > 0) {
            TravaBtns(true, false, false);
            Incluindo = false;
            //preenche od valores atuais no combobox 
            String  Strpro =(String) jcbxproduto.getSelectedItem();
            String  Strfor =(String) jcbxfornecedor.getSelectedItem();            
            CarregaCombobox();
            
            //reposiciona o combobox no registro que vai ser alterado
            utlp.getIndexComboboxSet(jcbxproduto,Strpro);
            utlp.getIndexComboboxSet(jcbxfornecedor,Strfor);

        } else {
            JOptionPane.showMessageDialog(null, "Nenhum registro Encontrado");
        }
    }//GEN-LAST:event_jbtnAlterarActionPerformed

    private void jcbxfornecedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbxfornecedorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbxfornecedorActionPerformed

    private void jtValorUNKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtValorUNKeyTyped
        String caracteres = "0987654321.";
        if (!caracteres.contains(evt.getKeyChar() + "")) {
            evt.consume();
        }
    }//GEN-LAST:event_jtValorUNKeyTyped

    private void jtQuantidadeKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtQuantidadeKeyTyped
        String caracteres = "0987654321";
        if (!caracteres.contains(evt.getKeyChar() + "")) {
            evt.consume();
        }
    }//GEN-LAST:event_jtQuantidadeKeyTyped

    private void jtValorTotalKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtValorTotalKeyTyped
        String caracteres = "0987654321.";
        if (!caracteres.contains(evt.getKeyChar() + "")) {
            evt.consume();
        }
    }//GEN-LAST:event_jtValorTotalKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormCompras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormCompras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormCompras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormCompras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FormCompras().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator Separador1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JButton jbtnAlterar;
    private javax.swing.JButton jbtnFirst;
    private javax.swing.JButton jbtnGravar;
    private javax.swing.JButton jbtnLast;
    private javax.swing.JButton jbtnNext;
    private javax.swing.JButton jbtnNovo;
    private javax.swing.JButton jbtnPesquisa;
    private javax.swing.JButton jbtnPrior;
    private javax.swing.JButton jbtncancelar;
    private javax.swing.JButton jbtnexcluir;
    private javax.swing.JComboBox jcbxfornecedor;
    private javax.swing.JComboBox jcbxproduto;
    private javax.swing.JComboBox jcbxprodutoPesq;
    private javax.swing.JPanel jpNavegacao;
    private javax.swing.JPanel jpnlPesq;
    private javax.swing.JFormattedTextField jtData;
    private javax.swing.JTextField jtQuantidade;
    private javax.swing.JTextField jtValorTotal;
    private javax.swing.JTextField jtValorUN;
    private javax.swing.JTextField jtcodigo;
    private javax.swing.JLabel labelCodigo;
    private javax.swing.JLabel labelCodigo1;
    private javax.swing.JLabel labelProdutos;
    private javax.swing.JLabel labelProdutos1;
    // End of variables declaration//GEN-END:variables
}
